﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SetupLauncher.Domain.Util;

namespace SetupLauncher.Domain.Logic.Environment
{
    internal class PreparePSEComponents : IPrepareCommand
    {
        Logger.ILoggable _log;

        public PreparePSEComponents(Logger.ILoggable log) 
        {
            _log = log;
        }

        public void Execute()
        {
            try
            {
                List<string> commands = new List<string>();

                commands.Add("sc stop \"Process Manager\"");
                commands.Add("sc delete \"Process Manager\"");
                commands.Add("sc stop \"BM&FBOVESPA Fix Communication\"");
                commands.Add("sc delete \"BM&FBOVESPA Fix Communication\"");
                commands.Add("sc stop \"PSEServiceHoster\"");
                commands.Add("sc delete \"PSEServiceHoster\"");
                commands.Add("sc stop \"PSE LoggingService\"");
                commands.Add("sc delete \"PSE LoggingService\"");


                string data = DateTime.Now.ToString("ddMMyyyyhhmmss");
                commands.Add(@"rename C:\DSEPROJ\Compilados\PSEComponents PSEComponents_old_" + data);

                foreach (string command in commands)
                {
                    WriteLog("Executando o comando: " + command);
                    DOSCommand.Execute(command);
                }

            }
            catch (Exception ex)
            {
                WriteException(ex.Message);
                throw ex;
            }
        }

        private void WriteLog(string message)
        {
            if (_log == null)
                return;
            _log.Write(message);
        }

        private void WriteException(string message)
        {
            if (_log == null)
                return;
            _log.WriteException(message);
        }
    }
}
